package com.neusoft.elm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.neusoft.elm.controller.BusinessController;
import com.neusoft.elm.po.Business;

@WebServlet("/BusinessController")
public class BusinessListServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    public BusinessListServlet() {
        super();
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doPost(request, response);
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        // 设置编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        
        String action = request.getParameter("action");
        PrintWriter out = response.getWriter();
        Gson gson = new Gson();
        
        try {
            BusinessController controller = new BusinessController();
            
            if ("list".equals(action)) {
                // 获取排序参数
                String orderBy = request.getParameter("orderBy");
                
                List<Business> businessList;
                if (orderBy != null && !"default".equals(orderBy)) {
                    // 创建新的请求对象来传递排序类型
                    HttpServletRequest sortRequest = new HttpServletRequestWrapper(request) {
                        @Override
                        public String getParameter(String name) {
                            if ("sortType".equals(name)) {
                                return orderBy;
                            }
                            return super.getParameter(name);
                        }
                    };
                    businessList = (List<Business>) controller.listBusinessBySort(sortRequest, response);
                } else {
                    businessList = (List<Business>) controller.listBusiness(request, response);
                }
                
                String json = gson.toJson(businessList);
                out.print(json);
                
            } else if ("getById".equals(action)) {
                Business business = (Business) controller.getBusinessById(request, response);
                String json = gson.toJson(business);
                out.print(json);
                
            } else {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                out.print("{\"error\":\"无效的action参数\"}");
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            out.print("{\"error\":\"服务器内部错误: " + e.getMessage() + "\"}");
        } finally {
            out.close();
        }
    }
}